﻿using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using TodoMVC.DBServices;
using TodoMVC.Models.BusinessModels;
using TodoMVC.Models.DBModels;

namespace TodoMVC.BusinessServices
{
    /// <summary>
    /// 基于数据层实现业务层次上的封装
    /// </summary>
    public class TodoMVCBusinessServices
    {
        private readonly TodoMVCDBServices _todoMVCDBServices;
        public TodoMVCBusinessServices(TodoMVCDBServices todoMVCDBServices)
        {
            _todoMVCDBServices = todoMVCDBServices;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        /// 
        public async Task<List<TodoItem>> GetTodoItemsAsync()
        {
            return await _todoMVCDBServices.GetTodoItemsAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<TodoItem> GetTodoItemAsync(long id)
        {
            return await _todoMVCDBServices.GetTodoItemByIdAsync(id);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="guid"></param>
        /// <returns></returns>
        public async Task<TodoItem> GetTodoItemAsync(Guid guid)
        {
            return await _todoMVCDBServices.GetTodoItemByGuidAsync(guid);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="todoItem"></param>
        /// <returns></returns>
        public async Task<bool> UpdateTodoItemAsync(TodoItem todoItem)
        {
            return await _todoMVCDBServices.UpdateTodoItemAsync(todoItem);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public async Task<bool> UpdateTodoItemAsync(long id, string content)
        {
            if (!await ExistTodoItemAsync(id))
            {
                return false;
            }
            var todoItem = await GetTodoItemAsync(id);

            todoItem.UpdateTime = DateTime.Now;
            todoItem.Content = content;
            return await UpdateTodoItemAsync(todoItem);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public async Task<bool> UpdateTodoItemAsync(long id, TodoItemStatus status)
        {
            if (!await ExistTodoItemAsync(id))
            {
                return false;
            }

            if (!Enum.IsDefined(status))
            {
                throw new ArgumentOutOfRangeException(nameof(status));
            }

            var todoItem = await GetTodoItemAsync(id);

            todoItem.UpdateTime = DateTime.Now;
            todoItem.TodoItemStatus = status;
            return await UpdateTodoItemAsync(todoItem);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="todoItem"></param>
        /// <returns></returns>
        public async Task<TodoItem> AddTodoItemAsync(TodoItem todoItem)
        {
            return await _todoMVCDBServices.AddTodoItemAsync(todoItem);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="todoItem"></param>
        /// <returns></returns>
        public async Task<TodoItem> AddTodoItemAsync(string content)
        {
            var todoItem = new TodoItem()
            {
                Guid = Guid.NewGuid(),
                CreateTime = DateTime.Now,
                UpdateTime = DateTime.Now,
                IsDeleted = false,
                TodoItemStatus = TodoItemStatus.Active,
                Content = content
            };

            return await AddTodoItemAsync(todoItem);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> DeleteTodoItemAsync(long id)
        {
            return await _todoMVCDBServices.DeleteTodoItemAsync(id);
        }

        public async Task<bool> DeleteTodoItemsAsync(List<long> ids)
        {
            return await _todoMVCDBServices.DeleteTodoItemsAsync(ids);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> ExistTodoItemAsync(long id)
        {
            return await _todoMVCDBServices.ExistTodoItemAsync(id);
        }
    }
}
